﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pipes.Queries.Model.Query
{
  public class StringLiteral : IExpression
  {
    public bool Deterministic { get { return true; } }
    public MetaData.GeneralDataType InferredType { get; set; }
    public string Value { get; set; }

    public StringLiteral() 
    {
      this.InferredType = MetaData.GeneralDataType.@string;
    }
    public StringLiteral(string value) : this()
    {
      if (value.StartsWith("N'"))
      {
        this.Value = value.Substring(2, value.Length - 3).Replace("''", "'");
      }
      else 
      {
        this.Value = value.Trim('\'').Replace("''", "'");
      }
    }

    public override string ToString()
    {
      return "'" + this.Value + "'";
    }

    public void Visit(Parser.IQueryVisitor visitor)
    {
      visitor.Visit(this);
    }

    public object Evaluate()
    {
      return this.Value;
    }

    public static implicit operator string(StringLiteral x)
    {
      return x.Value;
    }
    public static explicit operator DateTime(StringLiteral x)
    {
      return DateTime.Parse(x.Value);
    }
  }
}
